<?php
/**
 * @package Picasa Storage
 * @author Mike Gaul
 * @version 0.0.1
 */
/*
Plugin Name: Picasa Storage
Plugin URI: http://www.bymg.com/
Description: Auto upload the pictures to picasa.
Author: Mike Gaul
Version: 0.0.1
Author URI: http://www.bymg.com/
*/

define("MGPS_PLUGINURI", WP_CONTENT_URL.'/plugins/'.dirname(plugin_basename(__FILE__)));
define('MGPS_PLUGINDIR', dirname(__FILE__));
require_once MGPS_PLUGINDIR.'/functions.php';

// Width picasa can be display.
$gpicasa_widths = array(160, 320, 400, 512, 640);

add_filter('content_save_pre', 'mgps_filter', 8);
function mgps_filter($content)
{
    global $post_ID;
    
    if (empty($_POST['mgps_saveto_picasa_enable'])) return $content;
    if (empty($post_ID)) return $content;

    require_once MGPS_PLUGINDIR.'/mg-picasa-storage.class.php';

    $mgps = new mgPicasaStorage();
    return $mgps->doit($content);
}

add_action('admin_menu', 'mgps_admincp_menu');
function mgps_admincp_menu() {
    add_options_page(
        'Picasa Storage Options',
        'Picasa Storage',
        '6',
        'mgps_admincp_setting',
        'mgps_admincp_setting'
    );
}

function mgps_admincp_setting()
{
    global $gpicasa_widths;

    require_once MGPS_PLUGINDIR.'/mg-picasa-storage.class.php';
    require_once MGPS_PLUGINDIR.'/phpthumb/ThumbLib.inc.php';
    
    // update picasa account setting
    if (!empty($_POST['mgps_picasa_user'])) $mgps_picasa_user = trim($_POST['mgps_picasa_user']);
    if (!empty($_POST['mgps_picasa_pass'])) $mgps_picasa_pass = trim($_POST['mgps_picasa_pass']);
    if (!empty($mgps_picasa_user) && !empty($mgps_picasa_pass)) {
    	update_option('mgps_picasa_user', $mgps_picasa_user);
    	update_option('mgps_picasa_pass', $mgps_picasa_pass);
    }
    
    // update thumb sizes
    if (!empty($_POST['mgps_thumb_sizes'])) {
    	preg_match_all('/\d+x\d+/', $_POST['mgps_thumb_sizes'], $matches);
        
    	$mgps = new mgPicasaStorage();
    	foreach ($matches[0] as $size) {
    		$albumId = $mgps->createAlbumByName($size);
    		var_dump($albumId);
    	}
    	$mgps->createAlbumByName('original');
    	update_option('mgps_thumb_sizes', $matches[0]);
    }
    
    // save the custom thumb
    if (!empty($_FILES['mgps_custom_thumbfile']) && !empty($_POST['post_ID'])) {
    	if ($_FILES['mgps_custom_thumbfile']['type'] == 'image/jpeg') {
            global $post_ID;
    	    $post_ID = $_POST['post_ID'];
    	    
    	    $mgps = new mgPicasaStorage();
    	    
    	    list($width, $height) = explode('x', $_POST['mgps_custom_thumbsize']);
    		$thumbfile = $mgps->thumb($_FILES['mgps_custom_thumbfile']['tmp_name'], $width, $height);
            $uf = $mgps->upload("{$width}x{$height}", $thumbfile);
            
            // 保存到meta
            add_post_meta($post_ID, "thumb_{$width}x{$height}", $uf, true)
            or
            update_post_meta($post_ID, "thumb_{$width}x{$height}", $uf);
    	}
    }
    
    // save thumb width in content
    if (!empty($_POST['mgps_thumbwidth_in_content'])) {
    	update_option('mgps_thumbwidth_in_content', intval($_POST['mgps_thumbwidth_in_content']));
    }
?>
<div class="wrap">
    <h2>Picasa Storage设定</h2>
    <p>本插件用于将文章内的站外图片上传到Google Picasa上，并替换文章内原图片。</p>
    
    <hr/>
    
    <h3>Picasa帐号</h3>
    <form method="POST" enctype="multipart/form-data" action="" accept-charset="utf-8" >
        <table class="form-table"><tbody>
            <tr valign="top">
                <th scope="row"><label>用户名</label></th>
                <td>
                    <input type="text" class="regular-text code" value="<?php echo get_option('mgps_picasa_user');?>" id="mgps_picasa_user" name="mgps_picasa_user"/>
                </td>
            </tr>
            <tr valign="top">
                <th scope="row"><label>密码</label></th>
                <td>
                    <input type="text" class="regular-text code" value="<?php echo get_option('mgps_picasa_pass');?>" id="mgps_picasa_pass" name="mgps_picasa_pass"/>
                </td>
            </tr>
        </tbody></table>
        <p class="submit">
        	<input type="submit" value="保存Picasa帐号" class="button-primary" name="Submit"/>
        </p>
    </form>
    
    <h3>缩略图尺寸</h3>
    <p>如果设定缩略图的尺寸，每篇文章的第一张图片将按照下面尺寸分别生成缩略图。缩略图将根据尺寸存放在不同的相册中，例如<kbd>350x200</kbd>的缩略图将存放在命名为<kbd>350x200</kbd>的相册中。</p>
    <form method="POST" enctype="multipart/form-data" action="" accept-charset="utf-8" >
        <table class="form-table"><tbody>
            <tr valign="top">
                <th scope="row">
                    <label>尺寸列表</label>
                    <p>每行一组尺寸，形式为：宽x高。</p>
                    <p>例如：<br/>
                    350x200<br/>100x100<br/>250x250</p>
                </th>
                <td>
                    <textarea name="mgps_thumb_sizes" rows="6"><?php if ($sizes=get_option('mgps_thumb_sizes')) echo implode("\n", $sizes); ?></textarea>
                </td>
            </tr>
        </tbody></table>
        <p class="submit">
        	<input type="submit" value="保存缩略图尺寸" class="button-primary" name="Submit"/>
        </p>
    </form>
    
    <h3>文章图片尺寸</h3>
    <p>Picasa支持若干种图片宽度，例如<kbd>320</kbd>或<kbd>400</kbd>等。</p>
    <form method="POST" enctype="multipart/form-data" action="" accept-charset="utf-8" >
        <table class="form-table"><tbody>
            <tr valign="top">
                <th scope="row"><label>选择文章内图片宽度</label></th>
                <tr valign="top">
                    <th scope="row"><label>选择缩略图尺寸</label></th>
                    <td>
                        <select name="mgps_thumbwidth_in_content" id="mgps_thumbwidth_in_content">
                            <option value="">保持不变，使用原图</option>
                        <?php
                        $gpicasa_width = get_option('mgps_thumbwidth_in_content');
                        foreach ($gpicasa_widths as $width):
                            if ($gpicasa_width == $width) {
                            	echo "<option value=\"{$width}\" selected=\"selected\">{$width}</option>";
                            }else {
                            	echo "<option value=\"{$width}\">{$width}</option>";
                            }
                        ?>
                        <?php endforeach;?>
                        </select>
                    </td>
                </tr>
            </tr>
        </tbody></table>
        <p class="submit">
        	<input type="submit" value="保存文章图片尺寸" class="button-primary" name="Submit"/>
        </p>
    </form>
    
    <?php if (!empty($_GET['postid'])):?>
    <h3>自定义缩略图</h3><a name="custom_thumb"></a>
    <p></p>
    <form method="POST" enctype="multipart/form-data" action="" accept-charset="utf-8" >
        <table class="form-table"><tbody>
            <tr valign="top">
                <th scope="row"><label>选择缩略图尺寸</label></th>
                <td>
                    <select name="mgps_custom_thumbsize" id="mgps_custom_thumbsize">
                    <?php foreach ($sizes as $size):?>
                        <option value="<?php echo $size;?>"><?php echo $size;?></option>
                    <?php endforeach;?>
                    </select>
                </td>
            </tr>
            <tr valign="top">
                <th scope="row"><label>选择缩略图</label></th>
                <td><input size="50" type="file" value="" name="mgps_custom_thumbfile" id="mgps_custom_thumbfile"/></td>
            </tr>
        </tbody></table>
        <p class="submit">
        	<input type="hidden" value="<?php echo $_GET['postid'];?>" name="post_ID"/>
        	<input type="submit" value="保存自定义缩略图" class="button-primary" name="Submit"/>
        </p>
    </form>
    <?php endif;?>
</div>
<?php
}


function mgps_meta_box_content()
{
	global $post;
	
?>
<table>
    <tr>
        <td>
            <label for="mgps_saveto_picasa_enable">
                <input type="checkbox" value="doit" name="mgps_saveto_picasa_enable" id="mgps_saveto_picasa_enable"/>
                提交文章时将远程图片保存到Google Picasa中
            </label>
            <?php if (!empty($post->ID)):?>
            或
            <a href="<?php get_option('siteurl'); ?>/wp-admin/options-general.php?page=mgps_admincp_setting&postid=<?php echo $post->ID;?>#custom_thumb" target="_blank">点击这里自定义缩略图</a>
            <?php endif;?>
        </td>
    </tr>
</table>
<?php
}
function mgps_meta_box() {
	if ( function_exists('add_meta_box') ) {
		add_meta_box('mgps-custom-thumb', ' Custom Thumb', 'mgps_meta_box_content', 'post', 'advanced');
		add_meta_box('mgps-custom-thumb', ' Custom Thumb', 'mgps_meta_box_content', 'page', 'advanced');
	}
}
add_action('admin_menu', 'mgps_meta_box');
?>
